namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;

    public class Project
    {
        private string _CreatorDisplayName;
        private string _CreatorUserName;
        private DateTime _DateCreated;
        private string _Description;
        private int _Id;
        private string _ManagerDisplayName;
        private int _ManagerId;
        private string _Name;

        public Project(int id, string name, string description, int managerId, string creatorUsername) : this(id, name, description, managerId, string.Empty, creatorUsername, string.Empty, DefaultValues.GetDateTimeMinValue())
        {
        }

        public Project(int id, string name, string description, string managerDisplayName, string creatorDisplayName, DateTime dateCreated) : this(id, name, description, DefaultValues.GetUserIdMinValue(), managerDisplayName, string.Empty, creatorDisplayName, dateCreated)
        {
        }

        public Project(int id, string name, string description, int managerId, string managerDisplayName, string creatorUsername, string creatorDisplayName, DateTime dateCreated)
        {
            if ((name == null) || (name.Length == 0))
            {
                throw new ArgumentOutOfRangeException("name");
            }
            this._Id = id;
            this._Description = description;
            this._Name = name;
            this._ManagerId = managerId;
            this._ManagerDisplayName = managerDisplayName;
            this._CreatorUserName = creatorUsername;
            this._CreatorDisplayName = creatorDisplayName;
            this._DateCreated = dateCreated;
        }

        public static bool AddUserToProject(int userId, int projectId)
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().AddUserToProject(userId, projectId);
        }

        public static bool CloneProject(int projectId, string projectName)
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().CloneProject(projectId, projectName);
        }

        public bool Delete()
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteProject(this.Id);
        }

        public static bool DeleteProject(int projectId)
        {
            if (projectId <= DefaultValues.GetProjectIdMinValue())
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteProject(projectId);
        }

        public static ProjectCollection GetAllProjects()
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetAllProjects();
        }

        public static Project GetProjectById(int projectId)
        {
            if (projectId <= 0)
            {
                throw new ArgumentOutOfRangeException("projectId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetProjectById(projectId);
        }

        public static ProjectCollection GetProjectByMemberUsername(string username)
        {
            if (StringType.StrCmp(username, string.Empty, false) == 0)
            {
                throw new ArgumentOutOfRangeException("username");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetProjectByMemberUsername(username);
        }

        public static bool RemoveUserFromProject(int userId, int projectId)
        {
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().RemoveUserFromProject(userId, projectId);
        }

        public bool Save()
        {
            DataAccessLayerBaseClass DBLayer = DataAccessLayerBaseClassHelper.GetDataAccessLayer();
            if (this.Id <= DefaultValues.GetProjectIdMinValue())
            {
                int TempId = DBLayer.CreateNewProject(this);
                if (TempId > 0)
                {
                    this._Id = TempId;
                    return true;
                }
                return false;
            }
            return DBLayer.UpdateProject(this);
        }

        public string CreatorDisplayName
        {
            get
            {
                if ((this._CreatorDisplayName == null) || (this._CreatorDisplayName.Length == 0))
                {
                    return string.Empty;
                }
                return this._CreatorDisplayName;
            }
            set
            {
                this._CreatorDisplayName = value;
            }
        }

        public string CreatorUserName
        {
            get
            {
                return this._CreatorUserName;
            }
        }

        public DateTime DateCreated
        {
            get
            {
                return this._DateCreated;
            }
        }

        public string Description
        {
            get
            {
                if ((this._Description == null) || (this._Description.Length == 0))
                {
                    return string.Empty;
                }
                return this._Description;
            }
            set
            {
                this._Description = value;
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public string ManagerDisplayName
        {
            get
            {
                if ((this._ManagerDisplayName == null) || (this._ManagerDisplayName.Length == 0))
                {
                    return string.Empty;
                }
                return this._ManagerDisplayName;
            }
            set
            {
                this._ManagerDisplayName = value;
            }
        }

        public int ManagerId
        {
            get
            {
                return this._ManagerId;
            }
        }

        public string Name
        {
            get
            {
                if ((this._Name == null) || (this._Name.Length == 0))
                {
                    return string.Empty;
                }
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        public static bool SupportsProjectCloning
        {
            get
            {
                return DataAccessLayerBaseClassHelper.GetDataAccessLayer().SupportsProjectCloning;
            }
        }
    }
}

